﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;
using System.Drawing;

namespace PM.Site.Admin
{
    public partial class BankDeposits : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                    if (Session["BankDeposit"] != null)
                    {
                        BankDeposit bd = (BankDeposit)Session["BankDeposit"];
                        this.Loading(bd.ClientId, bd.BankId, bd.AccNo);
                    }
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading(string clientId, string bankId, string accNo)
        {
            if (!Page.IsPostBack)
            {
                BankDeposit bd = BankDepositServices.GetBankDepositByIDs(clientId, bankId, accNo);
                ddlClientIds.SelectedValue = bd.ClientId;
                ddlBankIds.SelectedValue = bd.BankId;
                txbAccNo.Text = bd.AccNo;
                ddlAccTypes.SelectedValue = bd.AccType;
                txbInvestValue.Text = bd.InvestValue.ToString();
                txbInvestDate.Text = bd.InvestDate.ToShortDateString();
                txbMaturityDate.Text = bd.MaturityDate.ToShortDateString();
                txbRate.Text = bd.Rate.ToString();
                chkbActive.Checked = bd.Active;

                ddlClientIds.Enabled = false;
                ddlBankIds.Enabled = false;
                txbAccNo.ReadOnly = true;
                txbAccNo.ForeColor = Color.FromArgb(172, 168, 153);
            }
        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                ddlClientIds.DataSource = ClientServices.GetClientID_Name_Collection();
                ddlClientIds.DataBind();
                ddlBankIds.DataSource = BankServices.GetAllBankCollectionWithBranch();
                ddlBankIds.DataBind();
                ddlAccTypes.DataSource = BankAccTypeServices.GetAllBankAccTypesCollection();
                ddlAccTypes.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            BankDeposit bd = new BankDeposit();
            bd.ClientId = ddlClientIds.SelectedItem.Value;
            bd.BankId = ddlBankIds.SelectedItem.Value;
            bd.AccNo = txbAccNo.Text;
            bd.AccType = ddlAccTypes.SelectedItem.Value;
            decimal investValue = 0.0M;
            decimal.TryParse(txbInvestValue.Text, out investValue);
            bd.InvestValue = investValue;
            DateTime investDate = DateTime.MinValue;
            if (DateTime.TryParse(txbInvestDate.Text, out investDate))
            {
                bd.InvestDate = investDate;
            }

            DateTime maturityDate = DateTime.MinValue;
            if (DateTime.TryParse(txbMaturityDate.Text, out maturityDate))
            {
                bd.MaturityDate = maturityDate;
            }
            decimal rate = 0.0M;
            decimal.TryParse(txbRate.Text, out rate);
            bd.Rate = rate;
            bd.Active = chkbActive.Checked;

            Session["BankDeposit"] = null;
            BankDepositServices.Add(bd);
            Response.Redirect("~/Admin/BankDepositsOverview.aspx");
        }
    }
}
